import { useState, useEffect } from 'react'

const useRowSelection = ({
  rowKey = 'id',
  multiple = true,
  disabledKeys = []
}) => {

  const [selectedRowKeys, setSelectedRowKeys] = useState([])
  
  const rowSelection = {
    type: multiple ? 'checkbox' : 'radio',
    selectedRowKeys,
    onChange: (selectedRowKeys) => {
      setSelectedRowKeys(selectedRowKeys)
    },
    getCheckboxProps: (rowData) => {
      return {
        disabled: disabledKeys.includes(rowData[rowKey])
      }
    }
  }

  return {
    selectedRowKeys,
    setSelectedRowKeys,
    rowSelection
  }

}

export default useRowSelection
